package 原型模式;

import java.io.*;

/**
 * @author xukang
 */
public class Product implements Cloneable,Serializable{

    private static final long serialVersionUID = -8885153238861968199L;
    private String part1;
    private String part2;
    private String part3;
    private String part4;
    private Demo demo;
    public Product() {
    }

    public Product(String part1, String part2, String part3, String part4,Demo demo) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
        this.demo = demo;
    }

    /**
     * 克隆拷贝
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Product clone() throws CloneNotSupportedException {
        Product product  = (Product)super.clone();
        Demo dloneDemo = this.demo.clone();
        product.setDemo(dloneDemo);
        return product;
    }

    /**
     * 序列化 拷贝2
     * @return
     * @throws CloneNotSupportedException
     */
    protected Product clone2() throws CloneNotSupportedException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        try {
            ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);
            Object obj = ois.readObject();
            return (Product)obj;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        return "Product{" +
                "["+this.hashCode()+"]"+
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                ", demo=" + demo +
                '}';
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public String getPart3() {
        return part3;
    }

    public void setPart3(String part3) {
        this.part3 = part3;
    }

    public String getPart4() {
        return part4;
    }

    public void setPart4(String part4) {
        this.part4 = part4;
    }

    public Demo getDemo() {
        return demo;
    }

    public void setDemo(Demo demo) {
        this.demo = demo;
    }
}
